<?php
/**
 * Created by PhpStorm.
 * User: hehecat
 * Date: 2018/7/10
 * Time: 13:20
 */

namespace app\api\model;


use think\Model;

class FollowModel extends  Model
{
    function follow($type, $id, $userid) {
        if ($type == 'club') {
            $is_exist_club = db('club')->where('clubid', $id)->find();
            if ($is_exist_club) {
                $is_follow = db('club_follow')->where('userid',$userid)->find();
                if (!$is_follow) {
                    $data = ['clubid' => $id, 'userid' => $userid];
                    $result = Db::name('club_follow')->insertGetId($data);
                    return ['success'=>$result];
                } else {
                    return  ['success'=>1];
                }
            }
            return  ['success'=>0];
        }else if ($type == 'master') {
            $is_exist_master = db('master')->where('masterid', $id)->find();
            if ($is_exist_master) {
                $is_follow = db('master_follow')->where('userid',$userid)->find();
                if (!$is_follow) {
                    $data = ['masterid' => $id, 'userid' => $userid];
                    $result = Db::name('master_follow')->insertGetId($data);
                    return ['success'=>$result];
                }else {
                    return  ['success'=>1];
                }
            }
            return  ['success'=>0];
        }
    }

    function unfollow($type, $id, $userid) {
        if ($type == 'club') {
            $data = ['clubid' => $id, 'userid' => $userid];
            $result = Db::name('club_follow')->delete($data);
            return ['id'=>$result];

        }else if ($type == 'master') {
            $data = ['masterid' => $id, 'userid' => $userid];
            $result = Db::name('master_follow')->delete($data);
            return ['id'=>$result];

        }else {
            return ['id'=>0];
        }
    }

    function followlist($userid) {
        $club_result = Db::table('club_follow')
            ->alias('follow')
            ->join('club',"club.clubid = follow.clubid and follow.userid = '$userid' ")
            ->select();
        $master_result = Db::table('master_follow')
            ->alias('follow')
            ->join('master',"master.clubid = master.clubid and follow.userid = '$userid' ")
            ->select();
        return ['followlist'=>['club'=>$club_result], ['master'=>$master_result]];
    }
}